﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MovieCollectionManager.DataModel;
using MovieCollectionManager.Util;

namespace MovieCollectionManager
{
    public partial class CreateUserForm : Form
    {
        public CreateUserForm()
        {
            InitializeComponent();
        }

        private void buttonCreateUser_Click(object sender, EventArgs e)
        {
            if (textBoxUsername.Text.Equals(""))
            {
                MessageBox.Show("Login do usuário não informado", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            if (textBoxPassword.Text.Equals(""))
            {
                MessageBox.Show("Senha do usuário não informada", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            if (textBoxPassword.Text != textBoxConfirmPassword.Text)
            {
                MessageBox.Show("Senhas não conferem!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            User newUser = new User();
            newUser.Name = textBoxUsername.Text;
            newUser.Password = Security.GenerateHash(textBoxPassword.Text);
            newUser.IsAdmin = checkBoxAdmin.Checked;

            var db = new MovieCollectionConteiner();
            db.Users.AddObject(newUser);
            db.SaveChanges();

            MessageBox.Show("Usuário cadastrado!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
            ClearFields();
            Close();
        }

        private void ClearFields()
        {
            textBoxUsername.Text = "";
            textBoxPassword.Text = "";
            textBoxConfirmPassword.Text = "";
            checkBoxAdmin.Checked = false;
        }

        private void buttonBack_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
